package com.financing.wap.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


/**
 * 发财指数
 * @author Marco
 *
 */
@SuppressWarnings("unchecked")
public class ActiviesMoneyUtils {
	static Logger logger = Logger.getLogger(ActiviesMoneyUtils.class);
	static Map<String,Integer> cacheMap = new HashMap<String,Integer>();
	
	public final static String ELEMENT_TITLE = "title";
	public final static String ELEMENT_CONTENT = "content";
	public final static String ELEMENT_SCORE = "score";
	public final static String ELEMENT_ANSWER = "a";
	public final static String ELEMENT_INDEX = "index";
	public final static String ELEMENT_ANSWER_LIST = "answerList";
	
	//题库list
	public static List<Map<Object,Object>> allQuestionsList = new ArrayList<Map<Object,Object>>();
	
	static{
		Logger logger = Logger.getLogger(MessageUtil.class);
		SAXReader  saxReader = new SAXReader();
		try {
			Document document = saxReader.read(MessageUtil.class.getResourceAsStream("/ActivitesMoenyQuestion.xml"));
			Element rootElement=document.getRootElement();  
			List<Element> qaList = rootElement.elements("qa");
			List<Element> qbList = rootElement.elements("qb");
			List<Element> qcList = rootElement.elements("qc");
			List<Element> qdList = rootElement.elements("qd");
			List<Element> qeList = rootElement.elements("qe");
			//所有xml中的题目Element对象
			List<Element> allQList = new ArrayList<Element>();
			allQList.addAll(qaList);
			allQList.addAll(qbList);
			allQList.addAll(qcList);
			allQList.addAll(qdList);
			allQList.addAll(qeList);
			
			for(Element qaElement:allQList){
				Map<Object,Object> qMap = new HashMap<Object,Object>();
				List<Map<Object,Object>> answerList = new ArrayList<Map<Object,Object>>();
				String index = qaElement.elementText(ELEMENT_INDEX);
				String title = qaElement.elementText(ELEMENT_TITLE);
				List<Element> aList = qaElement.elements(ELEMENT_ANSWER);
				for(Element aElement:aList){
					Map<Object,Object> answer = new HashMap<Object,Object>();
					String content = aElement.elementTextTrim(ELEMENT_CONTENT);
					String score = aElement.elementTextTrim(ELEMENT_SCORE);
					answer.put(ELEMENT_CONTENT, content);
					answer.put(ELEMENT_SCORE, score);
					answerList.add(answer);
				}
				qMap.put(ELEMENT_INDEX, index);
				qMap.put(ELEMENT_TITLE, title);
				qMap.put(ELEMENT_ANSWER_LIST, answerList);
				allQuestionsList.add(qMap);
			}
			
		} catch (DocumentException e) {
			logger.error("Init MessageTemplate error :", e);
			throw new RuntimeException(e);
		}
		
	}
	
	
	
	
	
	
	 
	
	public static void main(String[] orgs){
		 
		SAXReader  saxReader = new SAXReader();
		Document document;
		try {
			document = saxReader.read(MessageUtil.class.getResourceAsStream("/ActivitesMoenyQuestion.xml"));
			Element rootElement=document.getRootElement();  
			List<Element> qaList = rootElement.elements("qa");
			List<Element> qbList = rootElement.elements("qb");
			List<Element> qcList = rootElement.elements("qc");
			List<Element> qdList = rootElement.elements("qd");
			List<Element> qeList = rootElement.elements("qe");
			//所有xml中的题目Element对象
			List<Element> allQList = new ArrayList<Element>();
			allQList.addAll(qaList);
			allQList.addAll(qbList);
			allQList.addAll(qcList);
			allQList.addAll(qdList);
			allQList.addAll(qeList);
			//题库list
			List<Object> allQuestionsList = new ArrayList<Object>();
			for(Element qaElement:allQList){
				Map<Object,Object> qMap = new HashMap<Object,Object>();
				List<Map<Object,Object>> answerList = new ArrayList<Map<Object,Object>>();
				String index = qaElement.elementText(ELEMENT_INDEX);
				String title = qaElement.elementText(ELEMENT_TITLE);
				List<Element> aList = qaElement.elements(ELEMENT_ANSWER);
				for(Element aElement:aList){
					Map<Object,Object> answer = new HashMap<Object,Object>();
					String content = aElement.elementTextTrim(ELEMENT_CONTENT);
					String score = aElement.elementTextTrim(ELEMENT_SCORE);
					answer.put(ELEMENT_CONTENT, content);
					answer.put(ELEMENT_SCORE, score);
					answerList.add(answer);
				}
				qMap.put(ELEMENT_INDEX, index);
				qMap.put(ELEMENT_TITLE, title);
				qMap.put(ELEMENT_ANSWER_LIST, answerList);
				allQuestionsList.add(qMap);
			}
//			System.out.println(allQuestionsList);
		} catch (DocumentException e) {
			logger.error("Init ActivitesMoney error :", e);
			throw new RuntimeException(e);
		}
		
	}
}
